/*
 * @Description: 用户信息pinia
 * @Author: Zhanghan
 * @Date: 2024-12-06 10:37:12
 * @LastEditTime: 2024-12-06 11:25:45
 * @LastEditors: Zhanghan
 */
import { defineStore } from "pinia";
import { Local } from "@/enums/Local";
import { getLocal, getToken } from "@/utils/token";

type UserInfo = {
  userName: string;
  password: string;
};
interface UserState {
  userInfo: UserInfo | null;
  token?: string;
}

export const useUserStore = defineStore({
  id: "app-user",
  state: (): UserState => ({
    userInfo: null,
    token: undefined,
  }),
  getters: {
    getUserInfo(): UserInfo {
      return this.userInfo || getLocal(Local.USER_INFO);
    },
    getToken(): string {
      return (this.token as string) || getToken();
    },
  },
  actions: {
    setUserInfo(userInfo: any) {
      this.userInfo = userInfo;
    },
    setToken(token: string) {
      this.token = token;
    },
    resetState() {
      this.userInfo = null;
      this.token = undefined;
    },
  },
});
